﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using PCC.Framework.Utils;
using PCC.Framework.Biz;
using System.Data;
using System.Text;

namespace PCC.Framework.JWeb.Handler
{
    /// <summary>
    /// WellCatalogHandler 的摘要说明
    /// </summary>
    public class WellCatalogHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string parentId = string.IsNullOrEmpty(context.Request.QueryString["id"]) ? "-1" : context.Request.QueryString["id"];
            ArchiveBiz archiveBiz = new ArchiveBiz();
            DataTable wellCatalogTable = archiveBiz.QueryWellCatalogByParentId(parentId);
            StringBuilder resultStr = new StringBuilder("[");
            string iconCss = string.Empty;
            string ParentID = string.Empty;
            string jsonFormat = "{{\"id\": \"{0}\", \"text\": \"{1}\",\"pid\": \"{2}\", \"iconCls\": \"{3}\",  \"state\": \"{4}\",\"attributes\":{{\"typeCode\": \"{5}\"}}}},";
            int i = 1;
            string nodeState = "open";
            foreach (DataRow row in wellCatalogTable.Rows)
            {
                if (string.IsNullOrEmpty(row["pid"].ToString()))
                {
                    ParentID = "0";
                }
                else
                {
                    ParentID = row["pid"].ToString();
                }
                iconCss = ParentID == "-1" ? "icon-type" : "icon-folder";
                if (row["iswellcode"].ToString().Equals("1"))
                {
                    nodeState = "closed";
                }
                resultStr.Append(string.Format(jsonFormat, row["id"], row["text"], row["pid"], iconCss, nodeState, row["CATELOGSORT"]));
                i++;
            }
            if (resultStr.Length > 1)
            {
                resultStr = resultStr.Remove(resultStr.Length - 1, 1);
            }
            resultStr.Append("]");
            context.Response.ContentType = "text/plain";
            context.Response.Write(resultStr);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}